#######################################
##     Replicating Tables 3-5        ##
##       Political Scandals          ##
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## All Political Scandals

political_scandal <- subset(scandal, political == 1)

attach(political_scandal)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_political <- GenMatch(Tr = scandal_id, X = covariates_match, 
                      BalanceMatrix = covariates_balance, M = 3, 
                      pop.size = 12500, max.generations = 10, wait.generations = 5, 
                      hard.generation.limit = FALSE, int.seed = 374, unif.seed = 584, 
                      fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                      replace = TRUE, ties = FALSE)

effect_political_vote <- Match(Y = votepct, Tr = scandal_id, 
                         X = covariates_match, M = 3, 
                         exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                         replace = TRUE, ties = FALSE, version = "fast", 
                         Weight.matrix = match_political)
summary(effect_political_vote)

effect_political_win <- Match(Y = win, Tr = scandal_id, 
                                X = covariates_match, M = 3, 
                                exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                replace = TRUE, ties = FALSE, version = "fast", 
                                Weight.matrix = match_political, 
                                match.out = effect_political_vote)
summary(effect_political_win)

effect_political_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                          X = covariates_match, M = 3, 
                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                          replace = TRUE, ties = FALSE, version = "fast", 
                          Weight.matrix = match_political, 
                          match.out = effect_political_vote)
summary(effect_political_money)

balance_political <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                              dwnom1 + seniority + les + dpres + 
                              logged_pre_sum_infl + chall_quality, 
                            match.out = effect_political_vote,
                            nboots = 5000)

detach(political_scandal)

## Covered Political Scandals

political_scandal_covered <- subset(scandal, political == 1 & covered == 1)

attach(political_scandal_covered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_political_covered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                              BalanceMatrix = covariates_balance, M = 3, 
                              pop.size = 12500, max.generations = 10, wait.generations = 5, 
                              hard.generation.limit = FALSE, int.seed = 918, unif.seed = 390, 
                              fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                              replace = TRUE, ties = FALSE)

effect_political_vote_covered <- Match(Y = votepct, Tr = scandal_id, 
                                 X = covariates_match, M = 3, 
                                 exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                 replace = TRUE, ties = FALSE, version = "fast", 
                                 Weight.matrix = match_political_covered)
summary(effect_political_vote_covered)

effect_political_money_covered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                  X = covariates_match, M = 3, 
                                  exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                  replace = TRUE, ties = FALSE, version = "fast", 
                                  Weight.matrix = match_political_covered, 
                                  match.out = effect_political_vote_covered)
summary(effect_political_money_covered)



balance_political_covered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                      dwnom1 + seniority + les + dpres + 
                                      logged_pre_sum_infl + chall_quality, 
                                    match.out = effect_political_vote_covered,
                                    nboots = 5000)
detach(political_scandal_covered)

## Non-Covered Political Scandals

political_scandal_noncovered <- subset(scandal, political == 1 & covered == 0)

attach(political_scandal_noncovered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_political_noncovered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                 BalanceMatrix = covariates_balance, M = 3, 
                                 pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                 hard.generation.limit = FALSE, int.seed = 937, unif.seed = 347, 
                                 fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                 replace = TRUE, ties = FALSE)

effect_political_vote_noncovered <- Match(Y = votepct, Tr = scandal_id, 
                                    X = covariates_match, M = 3, 
                                    exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE, version = "fast", 
                                    Weight.matrix = match_political_noncovered)
summary(effect_political_vote_noncovered)

effect_political_money_noncovered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                     X = covariates_match, M = 3, 
                                     exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                     replace = TRUE, ties = FALSE, version = "fast", 
                                     Weight.matrix = match_political_noncovered, 
                                     match.out = effect_political_vote_noncovered)
summary(effect_political_money_noncovered)


balance_political_noncovered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                         dwnom1 + seniority + les + dpres + 
                                         logged_pre_sum_infl + chall_quality, 
                                       match.out = effect_political_vote_noncovered,
                                       nboots = 5000)

detach(political_scandal_noncovered)
